<?php
require_once("../../resources/commons.php");
require_once("../../data/User.php");
require_once("../../data/Report.php");
require_once("../../data/Item.php");

SessionManager::getInstance()->checkSession();
displayTop("Team history","../../resources/ajaxscripts.php",null);

/**
 * Display all the history for the specified user (last 10 reports)
 * @param Activity $p_record
 */
function displayTeamMemberHistory($p_record){
	?>
	<a href="team_history.php?id=<?=base64_encode($p_record->getId()) ?>">Display history</a>
	<?php
}
?>

<h1>My team</h1>
<?php
$l_myUserId=SessionManager::getCurrentLoggedUserId();
$l_user=User::getFromPrimaryKey($l_myUserId);
$l_teamMembers=$l_user->getFullTeam();

if(count($l_teamMembers)!=0){
	$l_teamSummaryTable=new TableData("TeamSummaryTable");
	$l_teamSummaryTable->addHeader("firstName","First name");
	$l_teamSummaryTable->addHeader("lastName","Last name");
	$l_teamSummaryTable->addAction("displayTeamMemberHistory");
	$l_teamSummaryTable->setRecords($l_teamMembers);
	displayTable($l_teamSummaryTable);
}else{
	?>
	<div style="color: white;background-color: red;">No team</div>
	<?php
}

//Check for the ID of a specified user
$l_teamMemberId=base64_decode($_REQUEST['id']);
if(isset($_REQUEST['id'])){
	$l_teamMember=User::getFromPrimaryKey($l_teamMemberId);
	if($l_teamMember->isUserManager($l_myUserId)){
?>
<h1><?=$l_teamMember->getFullName()?></h1>
<h2>Last 10 published reports</h2>
<?php
		$lastReports=Report::getFromCustomCriteria(
			"where User_id=:User_id order by createdDate desc limit 10",
			array("User_id" => $l_teamMemberId));
		if(count($lastReports)!=0){
			$l_teamMemberReportsTable=new TableData("TeamMemberReportsTable");
			$l_teamMemberReportsTable->addHeader("createdDate","Date");
			$l_teamMemberReportsTable->addHeader("notes","Content");
			$l_teamMemberReportsTable->setRecords($lastReports);
			displayTable($l_teamMemberReportsTable);
		}else{
			echo("None");
		}
	}
}
displayBottom();
?>